<?php

class Admin_Model_State {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_State();
    }

    /**
     * 
     * @return type
     */
    public function fetchAll() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function createNewState($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateState($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getStateById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = $id");
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $id
     * @param type $formated
     * @return type
     */
    public function getJustStateArray($id, $formated) {
        $cityById = $this->getStateById($id);
        $array = $this->getStateArray($cityById[0]);
        if ($formated) {
            $array = array('state' => $array);
        }
        return $array;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function getStateArray($data) {
        $array = array(
            'id' => $data['id'],
            'name' => $data['name'],
        );
        return $array;
    }

    /**
     * 
     * @return array
     */
    public function getAllStateArray() {
        $fetchAll = $this->fetchAll();
        $array = array();
        foreach ($fetchAll as $state) {                        
            $stateArray = $this->getStateArray($state);            
            array_push($array, $stateArray);
        }
        return $array;
    }

}

